/*
 * @Author: jimma0312 jimma0312@outlook.com
 * @Date: 2023-07-08 16:00:08
 * @LastEditors: Ma YuChen
 * @LastEditTime: 2023-08-03 10:26:38
 * @FilePath: \video-repeater-ctl-mcu\LT86101UXE\ctl_lt86101.c
 * @Description: 实现对于LT86101芯片控制逻辑
 */
 #include <stdio.h>
#include <gd32f10x_rcu.h>
#include "ctl_lt86101.h"
#include "bsp_i2c.h"
#include "systick.h"
#include "ctl_lt86101_gpio.h"


#define  EQ_NUMA  4
#define  EQ_NUMB  6
#define  EQ_NUMC  6
const uint8_t	l_1080EQ_level[EQ_NUMA]	= { 0x14, 0x18,0x28,0x08 };
const uint8_t	l_4K30EQ_level[EQ_NUMB]		= { 0x04,0x18,0x28, 0x48,0x08, 0x30 };
const uint8_t	l_4K60EQ_level[EQ_NUMC]		= { 0x78,0x38, 0x18,0x68, 0x28, 0x48 };
// const uint8_t	l_aEQ_level[EQ_NUM]		= { 0x0C, 0x14, 0x04,0x78,0x38, 0x18,0x68, 0x28, 0x48,0x08, 0x30, 0x50, 0x10, 0x60 };
// const uint8_t	l_aboost_level[4]	= { 0x01, 0x02, 0x04, 0x08 };

void vdInitLT86102UX( LT_GLOBAL* _lt_GlobalStruct );
void LT86104UX_RXOffsetCalibration( LT_GLOBAL* _lt_GlobalStruct );
bool bTxHpdDetect( LT_GLOBAL* _lt_GlobalStruct );
bool bRxHdmi5vStatusDet(void);
bool bTmdsClkStableDet( LT_GLOBAL* _lt_GlobalStruct, uint8_t addrs );
bool bHdmiVerChangeDet( LT_GLOBAL* _lt_GlobalStruct );
bool bTmdsClkChangeDet( LT_GLOBAL* _lt_GlobalStruct );
void vdTx_Drive_Config( LT_GLOBAL* _lt_GlobalStruct, ControlStatus State);
void vdHdmiVerStatusDet( LT_GLOBAL* _lt_GlobalStruct );
void vdRxPllStepSet( LT_GLOBAL* _lt_GlobalStruct );
void vdRxPllCalibration( LT_GLOBAL* _lt_GlobalStruct, uint8_t addr, uint8_t i );
void vdTxPllCalibration( LT_GLOBAL* _lt_GlobalStruct );
void Chip_Resolution_Det(LT_GLOBAL* _lt_GlobalStruct);
void vdTxStatusSet(LT_GLOBAL* _lt_GlobalStruct);
void Error_Counter( LT_GLOBAL* _lt_GlobalStruct );
void vdRxPllTestSet(  LT_GLOBAL* _lt_GlobalStruct );
uint32_t ulFreqIndicate(LT_GLOBAL* _lt_GlobalStruct);
void vdRxPllCalSet( LT_GLOBAL* _lt_GlobalStruct );
void LT86104UX_SetRXOfsVal( LT_GLOBAL* _lt_GlobalStruct );
void vdRxTermCalibration( LT_GLOBAL* _lt_GlobalStruct );
void vdTxTermCalibration( LT_GLOBAL* _lt_GlobalStruct );
uint32_t ulFreqIndicate1(LT_GLOBAL* _lt_GlobalStruct);
void Auto_EQ_1080P(LT_GLOBAL* _lt_GlobalStruct);
void Auto_EQ_4k60(LT_GLOBAL* _lt_GlobalStruct);
void Auto_EQ_4k30(LT_GLOBAL* _lt_GlobalStruct);
void vdRxRst( LT_GLOBAL* _lt_GlobalStruct );
void vdPiRst( LT_GLOBAL* _lt_GlobalStruct );

int8_t lt_WriteI2C_Byte(uint32_t i2c_periph, uint8_t reg, uint8_t data);
int8_t lt_ReadI2C_Byte(uint32_t i2c_periph, uint8_t reg);

/**
 * @description: 上电后，初始化lt86101芯片
 *               1.复位为低电平 延时 100ms
 *               2.复位为高电平 延时 100ms
 * 
 * @return {*}
 */
void lt86101_init(LT_GLOBAL* _lt_GlobalStruct)
{
    

    if((tickGet()-_lt_GlobalStruct->g_init_timer.s_lastTime) < _lt_GlobalStruct->g_init_timer.g_delayusTime)
    {
        return ;
    }

    _lt_GlobalStruct->g_init_timer.s_lastTime=tickGet();

    switch (_lt_GlobalStruct->g_init_timer.s_StageStep)
    {
        //开始复位芯片
    case 0:
    {
        lt8101_reset(ENABLE);
        _lt_GlobalStruct->g_init_timer.g_delayusTime = 100;
        _lt_GlobalStruct->g_init_timer.s_StageStep++;
        break;
    }
    //停止复位芯片，等待100ms
    case 1:
    {
        lt8101_reset(DISABLE);
        _lt_GlobalStruct->g_init_timer.g_delayusTime=100;
        _lt_GlobalStruct->g_init_timer.s_StageStep++;
        break;
    }
    case 2:
    {
        vdSet_Rx_HPD(_lt_GlobalStruct, DISABLE);
        vdInitLT86102UX(_lt_GlobalStruct);
        printf( "\r\nLT86102UX Init OK" );
        _lt_GlobalStruct->data_received[0]=0x0;
        _lt_GlobalStruct->data_received[1]=0x0;
        _lt_GlobalStruct->data_num=0;
        _lt_GlobalStruct->g_Flag_Init_Finished	= true;
        _lt_GlobalStruct->uOldTxPlugStatus[0]		= 0;
        _lt_GlobalStruct->g_ucTxPlugNumber		= 0;
        _lt_GlobalStruct->g_Startstage_clr[0]		=true;
        _lt_GlobalStruct->g_Startstage_clr[1]		=true;
        _lt_GlobalStruct->g_Startstage_clr[2]		=true;
        _lt_GlobalStruct->g_bFlagHdmi20True		= false;
        _lt_GlobalStruct->g_ucRxHdmi5vStatus		= true;


        _lt_GlobalStruct->g_init_timer.g_delayusTime=100;
        _lt_GlobalStruct->g_init_timer.s_StageStep=0;
        break;
    }
    default:
        break;
    }
}

void vStart_work(LT_GLOBAL* _lt_GlobalStruct)
{

    if((tickGet()-_lt_GlobalStruct->start_work_timer.s_lastTime) < _lt_GlobalStruct->start_work_timer.g_delayusTime)
    {
        return ;
    }
    _lt_GlobalStruct->start_work_timer.s_lastTime=tickGet();

    if(_lt_GlobalStruct->g_Startstage_clr[0])
    {
        _lt_GlobalStruct->start_work_timer.s_StageStep=0;
        _lt_GlobalStruct->g_Startstage_clr[0]=false;
    }
    switch (_lt_GlobalStruct->start_work_timer.s_StageStep)
    {
    case 0:
    {
        bTxHpdDetect(_lt_GlobalStruct);       // used to detect the hpd
        bRxHdmi5vStatusDet(); // used to detect the rx 5v
        if (_lt_GlobalStruct->FlagStartWork/* && g_ucRxHdmi5vStatus*/)
        {
            _lt_GlobalStruct->FlagStartWork = false;
            _lt_GlobalStruct->g_Systerm_start[0] = true;
            _lt_GlobalStruct->start_work_timer.s_StageStep++;
            _lt_GlobalStruct->start_work_timer.g_delayusTime = 500;
        }
        else
        {
            _lt_GlobalStruct->start_work_timer.s_StageStep = 0;
            _lt_GlobalStruct->start_work_timer.g_delayusTime = 100; // per 300ms detect the hpd and rx change once.
        }

        break;
    }
    case 1:
    {
        vdSet_Rx_HPD(_lt_GlobalStruct, ENABLE);
        _lt_GlobalStruct->start_work_timer.s_StageStep++;
        _lt_GlobalStruct->start_work_timer.g_delayusTime = 0;
        break;
    }
    case 2:
    {
        _lt_GlobalStruct->start_work_timer.s_StageStep++;
        _lt_GlobalStruct->start_work_timer.g_delayusTime = 100;
        break;
    }
    default:
        _lt_GlobalStruct->start_work_timer.s_StageStep	= 0;
        _lt_GlobalStruct->start_work_timer.g_delayusTime= 0;
        break;
    }
}

void CLK_det(LT_GLOBAL* _lt_GlobalStruct)
{
    if ( bTmdsClkStableDet(_lt_GlobalStruct, LT86101UXE )/*&&g_ucRxHdmi5vStatus */)//used to detect the clk is stable or not,Cycle detection
    {
        if ( bHdmiVerChangeDet(_lt_GlobalStruct) || bTmdsClkChangeDet(_lt_GlobalStruct) )
        {
            _lt_GlobalStruct->g_Startstage_clr[1]=true;

        }
        else
        {
            ;
        }
        _lt_GlobalStruct->Flag_ClkStable=true;
    }
    else
    {
        //printf( "\r\n CLK unstable" );
        vdTx_Drive_Config(_lt_GlobalStruct, DISABLE );
        _lt_GlobalStruct->Flag_ClkStable=false;
    }
}

void vdClk_Stable_Config(LT_GLOBAL* _lt_GlobalStruct)
{
    if((tickGet()-_lt_GlobalStruct->Clk_Stable_Cfg_timer.s_lastTime) < _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime)
    {
        return ;
    }
    _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_lastTime=tickGet();

    if(_lt_GlobalStruct->Flag_ClkStable==false)
    {
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep=0;
        return;
    }
    if(_lt_GlobalStruct->g_Startstage_clr[1])
    {
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep=0;
        _lt_GlobalStruct->g_Startstage_clr[1]=false;
    }

    switch (_lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep)
    {
    case 0:
        {
        //printf( "\r\n[%d] Config LT86101UXE", _lt_GlobalStruct->chipNo );
        vdTx_Drive_Config(_lt_GlobalStruct, ENABLE );
        vdHdmiVerStatusDet(_lt_GlobalStruct);//detect the hdmi version is hdmi 2.0 or hdmi 1.4
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep++;
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime = 0;
        break;
        }
    case 1:
    {
        //vdTxTermCalibration();
        if ( _lt_GlobalStruct->g_bFlagHdmi20True )
        {
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e, 0x66 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97, 0x66 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xE1, 0xf0 );       /*sofeware HDMI2.0 select//// */
        }
        else
        {
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e, 0x64 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97, 0x64 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xE1, 0xb0 );       /*sofeware HDMI1.4 select//// */
        }
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep++;
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime = 0;
        break;
    }
    case 2:
    {
        if(bTmdsClkChangeDet(_lt_GlobalStruct))
        {
            break;
        }
        vdRxPllStepSet(_lt_GlobalStruct);
        vdRxPllCalibration(_lt_GlobalStruct, LT86101UXE, 0 );
        vdTxPllCalibration(_lt_GlobalStruct);
        Chip_Resolution_Det(_lt_GlobalStruct);
        //lt_WriteI2C_Byte( 0xd5, 0x28 );                  /*set eq register */
        //lt_WriteI2C_Byte( 0xd6, 0x28);                  /*set eq register */
        //lt_WriteI2C_Byte( 0xd7, 0x28 );                  /*set eq register */
        //vdTxPllCalibration();
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep++;
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime		= 0;
        break;
    }
    case 3:
    {
        if ( lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x5c ) & 0x01 )
        {
            _lt_GlobalStruct->g_bFlagRxPllLock[0] = true;
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8a, 0x08 );
        }
        else
        {
            _lt_GlobalStruct->g_bFlagRxPllLock[0] = false;
        }
        if ( _lt_GlobalStruct->g_bFlagRxPllLock[0] )
        {
            vdTxStatusSet(_lt_GlobalStruct);
	     lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x19, 0x03 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x19, 0x00 );
            printf( "\r\n LT86101UXE PLL LOCK" );
            _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep		= 4;
            _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime		= 10;
            _lt_GlobalStruct->g_bFlagRxPllLock[0]	= false;
        }
        else
        {
            _lt_GlobalStruct->Clk_Stable_Cfg_timer.i++;
            if(_lt_GlobalStruct->Clk_Stable_Cfg_timer.i<20)
            {

                _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep	= 3;
                _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime	= 20;
            }
            else
            {
                _lt_GlobalStruct->Clk_Stable_Cfg_timer.i=0;
                _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep	= 4;
                _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime	= 10;
            }
        }
        break;
    }
    case 4:
    {
        //vdTxScdcWriteHandle( TX_ALL );
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep		= 4;
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime		= 20;
        break;
    }
    default:
            _lt_GlobalStruct->Clk_Stable_Cfg_timer.s_StageStep	= 0;
        _lt_GlobalStruct->Clk_Stable_Cfg_timer.g_delayusTime	= 0;
        break;
    }
}

void vdPrintError( LT_GLOBAL* _lt_GlobalStruct )
{
    _lt_GlobalStruct->printErr_timer.s_lastTime=0;
    _lt_GlobalStruct->printErr_timer.g_delayusTime=2000;

    if((tickGet()-_lt_GlobalStruct->printErr_timer.s_lastTime) < _lt_GlobalStruct->printErr_timer.g_delayusTime)
    {
        return ;
    }
    _lt_GlobalStruct->printErr_timer.g_delayusTime=tickGet();
    if ( _lt_GlobalStruct->g_bFlagHdmi20True )
    {
        printf( "\r\nCHIP A" );
        Error_Counter(_lt_GlobalStruct);
    }
    if((lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x54 ) & 0x03)==0x03)
    {
        //g_Systerm_start[0]	= true;
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x19, 0x03 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x19, 0x00 );
        printf( "\r\nCLK INT " );
    }
}

void Error_Counter( LT_GLOBAL* _lt_GlobalStruct )
{
    uint16_t	uieq_errcnt_d0	= 0x0000;
    uint16_t	uieq_errcnt_d1	= 0x0000;
    uint16_t	uieq_errcnt_d2	= 0x0000;

    uieq_errcnt_d0	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x67 );
    uieq_errcnt_d0	= uieq_errcnt_d0 << 8;
    uieq_errcnt_d0	= uieq_errcnt_d0 + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x66 );   /*read D0 error count */

    uieq_errcnt_d1	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x69 );
    uieq_errcnt_d1	= uieq_errcnt_d1 << 8;
    uieq_errcnt_d1	= uieq_errcnt_d1 + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x68 );   /*read D1 error count */

    uieq_errcnt_d2	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x6B );
    uieq_errcnt_d2	= uieq_errcnt_d2 << 8;
    uieq_errcnt_d2	= uieq_errcnt_d2 + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x6A );   /*read D2 error count */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xdf );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );//reset error counter
    printf( "\r\n%d D0:ERROR_COUNT =%x ",_lt_GlobalStruct->i2c_periph,(uint16_t)uieq_errcnt_d0 );
    printf( "\r\n%d D1:ERROR_COUNT =%x ",_lt_GlobalStruct->i2c_periph,(uint16_t) uieq_errcnt_d1 );
    printf( "\r\n%d D2:ERROR_COUNT =%x ",_lt_GlobalStruct->i2c_periph,(uint16_t)uieq_errcnt_d2 );
}

void vdRxPllCalibration( LT_GLOBAL* _lt_GlobalStruct, uint8_t addr, uint8_t i )
{
    uint32_t	ulFreqValue		= 0;
    uint32_t	ulPreFreqValue		= 0;
    uint8_t	ucPllCalibLevel_0	= 0x00;
    uint8_t	ucLevelMax		= 0x00;
    uint8_t	ucLevelMin		= 0x00;
    if(_lt_GlobalStruct->g_bFlagHdmi20True)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, 0x70 ); /*reset first level */
        vdRxPllTestSet(_lt_GlobalStruct);
        ulPreFreqValue = ulFreqIndicate(_lt_GlobalStruct);
        vdRxPllCalSet(_lt_GlobalStruct);
        delay_1ms( 3 );
        ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
        ulFreqValue	= ulFreqValue << 5;
#if 1
        if ( ulFreqValue == ulPreFreqValue )
        {
            ucPllCalibLevel_0 = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
        }
        else
        {
            if ( ulFreqValue < ulPreFreqValue )
            {
                while ( ulFreqValue < ulPreFreqValue )
                {
                    ucPllCalibLevel_0	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
                    ucPllCalibLevel_0	= ucPllCalibLevel_0 + 0x10;
                    if ( ucPllCalibLevel_0 == 0x00 )
                    {
                        ucPllCalibLevel_0 = 0xff;
                        break;
                    }
                    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                    //printf( "\r\nA0x89=%x" ,(uint16_t)lt_ReadI2C_Byte( 0x89 ));
                    delay_1ms( 3 );
                    ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                    ulFreqValue	= ulFreqValue << 5;/**32 */
                }
                ucLevelMax	= ucPllCalibLevel_0;
                ucLevelMin	= ucPllCalibLevel_0 - 0x10;
            }
            else if ( ulFreqValue > ulPreFreqValue )
            {
                while ( ulFreqValue > ulPreFreqValue )
                {
                    ucPllCalibLevel_0	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
                    ucPllCalibLevel_0	= ucPllCalibLevel_0 - 0x10;
                    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                    //printf( "\r\nB0x89=%x" ,(uint16_t)lt_ReadI2C_Byte( 0x89 ));
                    if ( ucPllCalibLevel_0 == 0xf0 )
                    {
                        ucPllCalibLevel_0 = 0x00;
                        break;
                    }
                    delay_1ms( 3 );
                    ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                    ulFreqValue	= ulFreqValue << 5;/**32 */
                }
                ucLevelMin	= ucPllCalibLevel_0;
                ucLevelMax	= ucPllCalibLevel_0 + 0x10;
            }
            do
            {
                ucPllCalibLevel_0 = (ucLevelMax + ucLevelMin) / 2;
                lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                //printf( "\r\nC0x89=%x" ,(uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ));
                delay_1ms( 5 );
                ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                ulFreqValue	= ulFreqValue << 5;/**32 */
                (ulFreqValue < ulPreFreqValue) ? (ucLevelMin = ucPllCalibLevel_0) : (ucLevelMax = ucPllCalibLevel_0);
            }
            while ( ( (ucLevelMax - ucLevelMin) > 0x01) && (ulFreqValue != ulPreFreqValue) );
        }
#endif
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, 0x10 ); /*reset first level */
        vdRxPllTestSet(_lt_GlobalStruct);
        ulPreFreqValue = ulFreqIndicate(_lt_GlobalStruct);
        vdRxPllCalSet(_lt_GlobalStruct);
        delay_1ms( 3 );
        ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
        ulFreqValue	= ulFreqValue << 5;
#if 1
        if ( ulFreqValue == ulPreFreqValue )
        {
            ucPllCalibLevel_0 = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
        }
        else
        {
            if ( ulFreqValue < ulPreFreqValue )
            {
                while ( ulFreqValue < ulPreFreqValue )
                {
                    ucPllCalibLevel_0	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
                    ucPllCalibLevel_0	= ucPllCalibLevel_0 + 0x10;
                    if ( ucPllCalibLevel_0 == 0x00 )
                    {
                        ucPllCalibLevel_0 = 0xff;
                        break;
                    }
                    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                    //printf( "\r\nC0x89=%x" ,(uint16_t)lt_ReadI2C_Byte( 0x89 ));
                    delay_1ms( 3 );
                    ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                    ulFreqValue	= ulFreqValue << 5;/**32 */
                }
                ucLevelMax	= ucPllCalibLevel_0;
                ucLevelMin	= ucPllCalibLevel_0 - 0x10;
            }
            else if ( ulFreqValue > ulPreFreqValue )
            {
                while ( ulFreqValue > ulPreFreqValue )
                {
                    ucPllCalibLevel_0	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89 ) & 0xf0;
                    ucPllCalibLevel_0	= ucPllCalibLevel_0 - 0x10;
                    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                    //printf( "\r\nD0x89=%x" ,(uint16_t)lt_ReadI2C_Byte( 0x89 ));
                    if ( ucPllCalibLevel_0 == 0xf0 )
                    {
                        ucPllCalibLevel_0 = 0x00;
                        break;
                    }
                    delay_1ms( 3 );
                    ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                    ulFreqValue	= ulFreqValue << 5;/**32 */
                }
                ucLevelMin	= ucPllCalibLevel_0;
                ucLevelMax	= ucPllCalibLevel_0 + 0x10;
            }
            do
            {
                ucPllCalibLevel_0 = (ucLevelMax + ucLevelMin) / 2;
                lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
                //printf( "\r\nE0x89=%x" ,(uint16_t)lt_ReadI2C_Byte( 0x89 ));
                delay_1ms( 5 );
                ulFreqValue	= ulFreqIndicate(_lt_GlobalStruct);
                ulFreqValue	= ulFreqValue << 5;/**32 */
                (ulFreqValue < ulPreFreqValue) ? (ucLevelMin = ucPllCalibLevel_0) : (ucLevelMax = ucPllCalibLevel_0);
            }
            while ( ( (ucLevelMax - ucLevelMin) > 0x04) && (ulFreqValue != ulPreFreqValue) );
        }
#endif
    }
    //printf( "\r\nPLL level=%x",(uint16_t)ucPllCalibLevel_0 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x89, ucPllCalibLevel_0 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x92, ucPllCalibLevel_0 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9b, ucPllCalibLevel_0 );
    printf( "\r\n[%d] PLL level=%x", _lt_GlobalStruct->chipNo, (uint16_t)ucPllCalibLevel_0 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x37, 0x0b );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcb, lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcb ) & 0xfb );/*this must be put before read 0x5c,or 0x5c will be error */
    delay_1ms( 10 );
    if ( lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x5c ) & 0x01 )
    {
        _lt_GlobalStruct->g_bFlagRxPllLock[i] = true;
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8a, 0x08 );
    }
    else
    {
        _lt_GlobalStruct->g_bFlagRxPllLock[i] = false;
    }
}

void vdSet_Rx_HPD(LT_GLOBAL* _lt_GlobalStruct, ControlStatus state)
{
    if(state)
    {
        /*use chip io control RX HPD */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe7, 0x08);
        printf("Set rx HPD high\r\n");
        //test_86101();
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe7, 0x48);
        printf("Set rx HPD low\r\n");
        //test_86101();
    }
}

void vdInitLT86102UX( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcb, 0x00 );       /*power on RX */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcd, 0x00 );       /*power on RX PI */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa1, 0x40 );       /*tx rst */
    //lt_WriteI2C_Byte( 0xaa, 0x40 );       /*tx rst */
    //lt_WriteI2C_Byte( 0xb3, 0x40 );       /*tx rst */
    //lt_WriteI2C_Byte( 0xbc, 0x40 );       /*tx rst */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc1, 0x00 );       /*tx tap0_en */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xb8, 0x00 );       /*tx tap0_en */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xaf, 0x00 );       /*tx tap0_en */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, 0x00 );       /*tx tap0_en */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x81, 0xcc );       /*ldo=1.3v */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x82, 0xcc );       /*ldo=1.3v */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe7, 0x48 );       /*Rx hpd Low,BIT2?BIT1???0,?????TX_ST???*/
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd1, 0x70 );       /*TX0_ST driving ability adjust */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa7, 0xf7 );
    //lt_WriteI2C_Byte( 0xb0, 0xf7 );
    LT86104UX_RXOffsetCalibration(_lt_GlobalStruct);
    LT86104UX_SetRXOfsVal(_lt_GlobalStruct);
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4, 0xa0 );                       /*Clock Dc in ???? */
    vdRxTermCalibration(_lt_GlobalStruct);//used to calibration the rterm
    vdTxTermCalibration(_lt_GlobalStruct);
}

void LT86104UX_RXOffsetCalibration( LT_GLOBAL* _lt_GlobalStruct )
{
   uint8_t read_data;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc3, 0x10 );               /*default value */
    /*D0 */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x04 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x14 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc3, 0x18 );               /*enable D0 channel offset calibration */
    delay_1ms( 15 );
    if ( !(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfe ) & 0x01) )      /*Offset calibration fail */
    {
        _lt_GlobalStruct->RX_OFFSET_VAL[0] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfb );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[0] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[0] = 0x0a;
        }
        else
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[0] = read_data;
        }
    }
    /*D1 */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x04 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x14 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc3, 0x14 );               /*enable D1 channel offset calibration */
    delay_1ms( 15 );
    if ( !(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfe ) & 0x02) )      /*Offset calibration fail */
    {
        _lt_GlobalStruct->RX_OFFSET_VAL[1] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfc );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[1] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[1] = 0x0a;
        }
        else
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[1] = read_data;
        }
    }
    /*D2 */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x04 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x14 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc3, 0x12 );               /*enable D2 channel offset calibration */
    delay_1ms( 15 );
    if ( !(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfe ) & 0x04) )      /*Offset calibration fail */
    {
        _lt_GlobalStruct->RX_OFFSET_VAL[2] = 0;
    }
    else                                            /*Offset calibration pass */
    {
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xfd );
        if ( (read_data & 0xf0) && ( (read_data & 0xf0) >= 0xa0) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[2] = 0xa0;
        }
        else if ( (read_data & 0x0f) && ( (read_data & 0x0f) >= 0x0a) )
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[2] = 0x0a;
        }
        else
        {
            _lt_GlobalStruct->RX_OFFSET_VAL[2] = read_data;
        }
    }
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc3, 0x10 ); /*set to default value */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xea, 0x20 );    
}

void LT86104UX_SetRXOfsVal( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xda, _lt_GlobalStruct->RX_OFFSET_VAL[0] );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xdb, _lt_GlobalStruct->RX_OFFSET_VAL[1] );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xdc, _lt_GlobalStruct->RX_OFFSET_VAL[2] );
	printf( "\r\n[%d] D0:RX_OFFSET_VAL =%x ",_lt_GlobalStruct->chipNo,(uint16_t)_lt_GlobalStruct->RX_OFFSET_VAL[0] );
	printf( "\r\n[%d] D1:RX_OFFSET_VAL =%x ",_lt_GlobalStruct->chipNo,(uint16_t)_lt_GlobalStruct->RX_OFFSET_VAL[1] );
	printf( "\r\n[%d] D2:RX_OFFSET_VAL =%x ",_lt_GlobalStruct->chipNo,(uint16_t)_lt_GlobalStruct->RX_OFFSET_VAL[2] );
}

void vdTxTermCalibration( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa0, 0x50 );
    //lt_WriteI2C_Byte( 0xa9, 0x50 );
    //lt_WriteI2C_Byte( 0xb2, 0x50 );
    //lt_WriteI2C_Byte( 0xbb, 0x50 );
    /*enable auto PD */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9f, 0x4a );
    //lt_WriteI2C_Byte( 0xa8, 0x4a );
    //lt_WriteI2C_Byte( 0xb1, 0x4a );
    //lt_WriteI2C_Byte( 0xba, 0x4a );
    /*rst */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9f, 0x4b );
    //lt_WriteI2C_Byte( 0xa8, 0x4b );
    //lt_WriteI2C_Byte( 0xb1, 0x4b );
    //lt_WriteI2C_Byte( 0xba, 0x4b );
}

bool bRxHdmi5vStatusDet(void)
{
    //TODo 无法探知 5V变化
    return false;

    #if 0
    if ( g_ucRxHdmi5vStatus == New_Rx_5V )
    {
        return(false);
    }
    g_ucRxHdmi5vStatus = New_Rx_5V;
    if ( New_Rx_5V )
    {
        delay_1ms( 10 );
        if ( New_Rx_5V )
        {
            g_bFlagSystemStart = true;
            printf( "\r\n RX 5V HIGH!" );
        }
        return(true);
    }
    else
    {
        printf( "\r\n RX 5V LOW!" );
        vdSet_RX_HPD( Low );
        g_Flag_Init_Finished = false;
    }
    return(false);
    #endif 
}

bool bTxHpdDetect(LT_GLOBAL* _lt_GlobalStruct)
{
    uint8_t loopy=0;
    uint8_t	newtxhpdstatus[2] = { 0, 0 };
    uint8_t newhpd=0,oldhpd=0;

    newtxhpdstatus[0]=lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xf5) & 0x0f;//use chip to detect  the hpd,we can read the register 0xf5.
    if (newtxhpdstatus[0] == _lt_GlobalStruct->uOldTxPlugStatus[0])
    {
        return false;
    }

    for (loopy = 0; loopy < 4; loopy++)
    {
        newhpd=newtxhpdstatus[0] & (1<<loopy);
        oldhpd=_lt_GlobalStruct->uOldTxPlugStatus[0] & (1<<loopy);

        if(newhpd != oldhpd)
        {
            if((oldhpd==0) && (newhpd != 0))
            {
                _lt_GlobalStruct->g_ucTxPlugNumber++;
                if(_lt_GlobalStruct->g_ucTxPlugNumber==1)
                {
                    _lt_GlobalStruct->FlagStartWork=true;
                }
            }
            else
            {
                _lt_GlobalStruct->g_ucTxPlugNumber--;
            }
        }
    }
    
    if(_lt_GlobalStruct->g_ucTxPlugNumber == 0)
    {
        _lt_GlobalStruct->FlagStartWork=false;
        vdSet_Rx_HPD(_lt_GlobalStruct, DISABLE);
        _lt_GlobalStruct->g_Flag_Init_Finished=false;
    }
    else
    {
        printf("\r\n[%d] Tx_Changed",_lt_GlobalStruct->chipNo);
    }
    _lt_GlobalStruct->uOldTxPlugStatus[0]=newtxhpdstatus[0];
    return true;
}

bool bTmdsClkStableDet(LT_GLOBAL* _lt_GlobalStruct, uint8_t addrs )
{
    uint8_t	ucClkStatus	= 0x00;
    uint32_t	ulPreTmdsClk	= 0;
    uint32_t	ulTmdsClk	= 0;
    ucClkStatus	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xF0 ) & 0x20;     /*Clk stable */
    ulPreTmdsClk	= ulFreqIndicate(_lt_GlobalStruct);
    delay_1ms(3);
    ulTmdsClk = ulFreqIndicate(_lt_GlobalStruct);
    if ( ucClkStatus )
    {
        if ( ulPreTmdsClk >= ulTmdsClk )
        {
            if ( (ulPreTmdsClk - ulTmdsClk) <= CLK_CHANGE_MARGIN )
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else
        {
            if ( (ulTmdsClk - ulPreTmdsClk) <= CLK_CHANGE_MARGIN )
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
    }
    return(false);
}

void vdTx_Drive_Config(LT_GLOBAL* _lt_GlobalStruct, ControlStatus State)
{
    uint8_t read_data;
    if (State)
    {
        if (_lt_GlobalStruct->uOldTxPlugStatus[0] & 0x01)
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6);
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, (read_data & 0x10) | 0xa0);
            //printf("\r\n[%d] TX0 ON", _lt_GlobalStruct->chipNo);
        }
        else
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6);
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, read_data & 0x10);
            //printf("\r\n[%d] TX0 OFF", _lt_GlobalStruct->chipNo);
        }
#if 0
        if ( uOldTxPlugStatus[0] & 0x02 )
        {
            read_data = lt_ReadI2C_Byte( 0xaf );
            lt_WriteI2C_Byte( 0xaf, (read_data & 0x10) | 0xa0 );
            printf( "\r\nTX1 ON" );
        }
        else
        {
            read_data = lt_ReadI2C_Byte( 0xaf );
            lt_WriteI2C_Byte( 0xaf, read_data & 0x10 );
            printf( "\r\nTX1 OFF" );
        }
        if ( uOldTxPlugStatus[0] & 0x04 )
        {
            read_data = lt_ReadI2C_Byte( 0xb8 );
            lt_WriteI2C_Byte( 0xb8, (read_data & 0x10) | 0xa0 );
            printf( "\r\nTX2 ON" );
        }
        else
        {
            read_data = lt_ReadI2C_Byte( 0xb8 );
            lt_WriteI2C_Byte( 0xb8, read_data & 0x10 );
            printf( "\r\nTX2 OFF" );
        }
        if ( uOldTxPlugStatus[0] & 0x08 )
        {
            read_data = lt_ReadI2C_Byte( 0xc1 );
            lt_WriteI2C_Byte( 0xc1, (read_data & 0x10) | 0xa0 );
            printf( "\r\nTX3 ON" );
        }
        else
        {
            read_data = lt_ReadI2C_Byte( 0xc1 );
            lt_WriteI2C_Byte( 0xc1, read_data & 0x10 );
            printf( "\r\nTX3 OFF" );
        }
#endif
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, 0x10);
    }
}

uint32_t ulFreqIndicate(LT_GLOBAL* _lt_GlobalStruct)
{
    uint32_t	Value		= 0;
    uint8_t	TempValue	= 0x00;
    uint8_t   read_value=0x00;
    read_value=lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x06 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph,  0x06, read_value&0xdf );
    Value		= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,  0xf2 ) & 0x07;
    Value		= Value << 8;
    TempValue	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,  0xf3 );
    Value		|= TempValue;
    Value		= Value << 8;
    TempValue	= 0x00;
    TempValue	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,  0xf4 );
    Value		|= TempValue;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph,  0x06, read_value|0x20 );
    return(Value);
}

bool bHdmiVerChangeDet(LT_GLOBAL* _lt_GlobalStruct)
{
    if (_lt_GlobalStruct->g_bFlagHdmi20True )                                        /*Hdmi version change */
    {
        if ( (_lt_GlobalStruct->data_received[0]==0x20)&&((_lt_GlobalStruct->data_received[1] & 0x02) == 0x02) )       /*SCDC_REG20,TMDS_Bit_Clock_Ratio:?source??,0:1/10;1:1/40? */
        {
            return(false);
        }
        else
        {
            printf( "\r\n[%d] HdmiVerChangeDet=true" , _lt_GlobalStruct->chipNo);
            return(true);
        }
    }
    else
    {
        if ( (_lt_GlobalStruct->data_received[0]==0x20)&&((_lt_GlobalStruct->data_received[1] & 0x02) == 0x02) )       /*SCDC_REG20 */
        {
            printf( "\r\n[%d] HdmiVerChangeDet=true", _lt_GlobalStruct->chipNo );
            return(true);
        }
        else
        {
            return(false);
        }
    }
}

bool bTmdsClkChangeDet( LT_GLOBAL* _lt_GlobalStruct )
{
    uint32_t		ulTmdsClock;
    if ( _lt_GlobalStruct->g_Systerm_start[0] == true )
    {
        _lt_GlobalStruct->s_ulPreTmdsClock	= 0;
        _lt_GlobalStruct->g_Systerm_start[0]	= false;
    }
    ulTmdsClock = ulFreqIndicate1(_lt_GlobalStruct);
    if ( ulTmdsClock < CLK_ACTIVE_MIN_LIMIT )
    {
        //s_ulPreTmdsClock = ulTmdsClock;
        return(false);
    }
    if ( ulTmdsClock >= _lt_GlobalStruct->s_ulPreTmdsClock )
    {
        if ( ulTmdsClock - _lt_GlobalStruct->s_ulPreTmdsClock >= CLK_CHANGE_MARGIN )
        {
            _lt_GlobalStruct->s_ulPreTmdsClock = ulTmdsClock;
            //printf( "\r\n clk det1" );
            return(true);
        }
        else
        {
            _lt_GlobalStruct->s_ulPreTmdsClock = ulTmdsClock;
            return(false);
        }
    }
    else
    {
        if ( _lt_GlobalStruct->s_ulPreTmdsClock - ulTmdsClock >= CLK_CHANGE_MARGIN )
        {
            _lt_GlobalStruct->s_ulPreTmdsClock = ulTmdsClock;
            //printf( "\r\n clk det2" );
            return(true);
        }
        else
        {
            _lt_GlobalStruct->s_ulPreTmdsClock = ulTmdsClock;
            return(false);
        }
    }
}

void vdRxPllStepSet( LT_GLOBAL* _lt_GlobalStruct )
{
    uint8_t	read_data;
    uint32_t	ulclk_freq;

    if ( _lt_GlobalStruct->g_bFlagHdmi20True )
    {
        //printf( "\r\nRX PLL2.0 SET" );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x86, 0x14 );       /*xxPLL_CPCUR_SEL */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x87, 0x50 );       /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8f, 0x14 );       /*xxPLL_CPCUR_SEL */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x90, 0x50 );       /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        //lt_WriteI2C_Byte( 0xc3, 0x10 );
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4, read_data & 0xf8 );
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc, (read_data & 0xf8) | 0x04 );
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5, (read_data & 0x80) | 0x07 );
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6, read_data & 0xfc );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xdd, 0x02 );
        //read_data = lt_ReadI2C_Byte( 0x85 );
        //lt_WriteI2C_Byte( 0x85, read_data | 0x20 );                               /*RXPLL_FREQ_SEL */
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, read_data & 0xf3 );                   /*rxpll_freq_set<1:0> */
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91, read_data & 0xf3 );                   /*tx1pll_freq_set<1:0> */
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a, read_data & 0xf3 );                   /*tx2pll_freq_set<1:0> */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xce, 0x01 );                               /*bit0:RG_RXPI_SEL bit1:RXPI high current power down */
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x86, 0x18 );                               /*xxPLL_CPCUR_SEL */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x87, 0x34 );                               /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8f, 0x11 );                               /*xxPLL_CPCUR_SEL */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x90, 0x50 );                               /*xxPLL_LPF_RSET  xxPLL_LPF_CPSET */
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5, 0x7f );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xdd, 0x0a );
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x85 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x85, read_data | 0x20 );                               /*RXPLL_FREQ_SEL */
        read_data	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88 );
        ulclk_freq	= ulFreqIndicate(_lt_GlobalStruct);
        if ( ulclk_freq > 0x2ee00 )                                     /* 0x2ee00 = 192000 */
        {
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, read_data & 0xf3 );           /*rxpll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x17700) && (ulclk_freq <= 0x2ee00) )   /* 0x17700 = 96000 */
        {
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, (read_data & 0xf3) | 0x08 );  /*rxpll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x40d8) && (ulclk_freq <= 0x17700) )    /* 0x40d8 = 16600 */
        {
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, read_data | 0x0c );           /*rxpll_freq_set<1:0> */
        }
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e, read_data | 0x20 );                   /*TX1PLL_FREQ_SEL */
        read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97, read_data | 0x20 );                   /*TX2PLL_FREQ_SEL */
        read_data	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91 );
        read_data	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a );
        if ( ulclk_freq > 0x2ee00 )                                     /* 0x2ee00 = 192000 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91, read_data & 0xf3 );           /*tx1pll_freq_set<1:0> */
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a, read_data & 0xf3 );           /*tx2pll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x17700) && (ulclk_freq <= 0x2ee00) )   /* 0x17700 = 96000 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91, (read_data & 0xf3) | 0x08 );  /*tx1pll_freq_set<1:0> */
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a, (read_data & 0xf3) | 0x08 );  /*tx2pll_freq_set<1:0> */
        }
        else if ( (ulclk_freq > 0x40d8) && (ulclk_freq <= 0x17700) )    /* 0x40d8 = 16600 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x91, read_data | 0x0c );           /*tx1pll_freq_set<1:0> */
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9a, read_data | 0x0c );           /*tx2pll_freq_set<1:0> */
        }
        if ( ulclk_freq > 0x33450 )                                     /* 0x33450 = 210000 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4, (read_data & 0xf8) | 0x01 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc, (read_data & 0xf8) | 0x02 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5, (read_data & 0x80) | 0x0b );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6, (read_data & 0xfc) | 0x01 );
        }
        else if ( (ulclk_freq > 0x19E10) && (ulclk_freq <= 0x33450) )  /* 0x19E10  = 106000 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4, (read_data & 0xf8) | 0x02 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc, (read_data & 0xf8) | 0x00 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5, (read_data & 0x80) | 0x09 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6, (read_data & 0xfc) | 0x02 );
        }
        else    /*if( (clk_freq > 0x40d8) && (clk_freq <= 0x19E10) ){// 0x40d8 = 16600 */
        {
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc4, (read_data & 0xf8) | 0x03 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcc, (read_data & 0xf8) | 0x00 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc5, (read_data & 0x80) | 0x08 );
            read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6 );
            lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc6, (read_data & 0xfc) | 0x03 );
        }
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xce, 0x03 ); /*bit0:RG_RXPI_SEL bit1:RXPI high current power down */
    }
}

void vdRxPllTestSet( LT_GLOBAL* _lt_GlobalStruct )
{
    uint8_t ucreddata;
    //lt_WriteI2C_Byte( 0xc1, 0x00 );
    //lt_WriteI2C_Byte( 0xb8, 0x00 );
    //lt_WriteI2C_Byte( 0xaf, 0x00 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, 0x00 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcb, 0x00 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x84, 0xc1 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8b, 0x48 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x83, 0x00 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xef );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );
    ucreddata = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, ucreddata | 0x01 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x88, ucreddata & 0xFE );
}

void vdRxPllCalSet( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x37, 0x1b );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xef );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );
#if 1
    if ( _lt_GlobalStruct->g_bFlagHdmi20True )
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x85, 0x66 );
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x85, 0x64 );
    }
#endif
    //lt_WriteI2C_Byte( 0x85, 0x26 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8a, 0x4a );
    /*	lt_WriteI2C_Byte( 0xcc,0x00  ); */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xca, 0x01 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xcb, 0x06 );
}

void vdTxPllCalibration( LT_GLOBAL* _lt_GlobalStruct )
{
    /*Tx Pll 1 */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e, lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8e ) | 0x24 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x33, lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x30 ) );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8C, 0xc0 );       /*disable calibratin */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8D, 0x41 );       /*RXPLL_VREF_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x94, 0x08 );       /*RXPLL_VREG25_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8D, 0xc1 );       /*RXPLL_VREG_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8C, 0x40 );       /*RXPLL_BIAS normal */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x8C, 0x00 );       /*RXPLL_PD  normal */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xef );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );       /*RXPLL reset */


    /*
     * lt_WriteI2C_Byte(0x8C,0x10);// calibratin Enable
     * Tx Pll 2
     */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97, lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x97 ) | 0x24 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x35, lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x30 ) );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x95, 0xc0 );       /*disable calibratin */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x96, 0x41 );       /*RXPLL_VREF_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x9d, 0x08 );       /*RXPLL_VREG25_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x96, 0xc1 );       /*RXPLL_VREG_EN */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x95, 0x40 );       /*RXPLL_BIAS normal */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x95, 0x00 );       /*RXPLL_PD  normal */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xdf );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );       /*RXPLL reset */
    /*lt_WriteI2C_Byte(0x95,0x10);// calibratin Enable */
}

void Chip_Resolution_Det(LT_GLOBAL* _lt_GlobalStruct)
{
	uint32_t fre=0;
	fre=ulFreqIndicate(_lt_GlobalStruct);
	if(_lt_GlobalStruct->g_bFlagHdmi20True)
	{
		Auto_EQ_4k60(_lt_GlobalStruct);
	}
	else if(fre> 0x2ee00)
	{
		Auto_EQ_4k30(_lt_GlobalStruct);
	}
	else if ((fre > 0x17700) && (fre <= 0x2ee00))
	{
		Auto_EQ_1080P(_lt_GlobalStruct);
	}
	else
	{
	vdRxRst(_lt_GlobalStruct);
    	vdPiRst(_lt_GlobalStruct);
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, 0x28 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, 0x28 );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, 0x28 );	
	}
}

void Auto_EQ_1080P(LT_GLOBAL* _lt_GlobalStruct)
{
    uint8_t i=0;
    uint8_t eq_stage[3]={0};
    uint8_t eq_count[3]={0};
    uint8_t eq_store[3][EQ_NUMA]={0};
    uint8_t ucread_var=0;
    uint16_t uieq_errcnt_d0[EQ_NUMA]={0xffff};
   // uint16_t uieq_errcnt_d1[EQ_NUMA]=0xffff;
    //uint16_t uieq_errcnt_d2[EQ_NUMA]=0xffff;
    vdRxRst(_lt_GlobalStruct);
    vdPiRst(_lt_GlobalStruct);
    ucread_var = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23 ) | 0x02;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23, ucread_var );
    do
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_1080EQ_level[eq_stage[0]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_1080EQ_level[eq_stage[1]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_1080EQ_level[eq_stage[2]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xdf ); //reset error counter
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );
        delay_1ms(10);
        if(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]]	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x67 );
            //printf( "\r\nm=%x",(uint16_t)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x66 );   /*read D0 error count */

            //uieq_errcnt_d1[eq_stage[1]]	= lt_ReadI2C_Byte( 0x69 );
           // uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] << 8;
           // uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] + lt_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]	= lt_ReadI2C_Byte( 0x6b );
           // uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] << 8;
            //uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] + lt_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
        eq_stage[1]++;
       eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMA);
    for(i=0; i<EQ_NUMA; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
	//if(uieq_errcnt_d1[i]==0x0000)
       // {
          //  eq_store[1][eq_count[1]]=i;
           // eq_count[1]++;
      //  }
	// if(uieq_errcnt_d2[i]==0x0000)
      //  {
        //    eq_store[2][eq_count[2]]=i;
        //    eq_count[2]++;
      //  }
    }
    //printf( "\r\nd0 eq=%x",(uint16_t)eq_count[0] );
    if(eq_count[0]==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_1080EQ_level[1] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_1080EQ_level[1] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_1080EQ_level[1] );
    }
    else if(eq_count[0]%2==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_1080EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_1080EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[1] );
   // if(eq_count[1]==0)
   // {
      //  lt_WriteI2C_Byte( 0xd6, l_1080EQ_level[1] );
  //  }
  //  else if(eq_count[1]%2==0)
  //  {
        //lt_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[1][(eq_count[1]/2)]] );
 //   }
  //  else
   // {
   //     lt_WriteI2C_Byte( 0xd6, l_1080EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
  //  }
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[2] );
   // if(eq_count[2]==0)
   // {
     //   lt_WriteI2C_Byte( 0xd7, l_1080EQ_level[1] );
  //  }
   // else if(eq_count[2]%2==0)
   // {
   //     lt_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[2][(eq_count[2]/2)]] );
  //  }
  //  else
   // {
    //    lt_WriteI2C_Byte( 0xd7, l_1080EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
    //}
    printf( "\r\n[%d] d0 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,0xd5) );
    printf( "\r\n[%d] d1 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,0xd6) );
    printf( "\r\n[%d] d2 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph,0xd7) );
}

void Auto_EQ_4k30(LT_GLOBAL* _lt_GlobalStruct)
{
    uint8_t i=0;
    uint8_t eq_stage[3]={0};
    uint8_t eq_count[3]={0};
    uint8_t eq_store[3][EQ_NUMB]={0};
    uint8_t ucread_var=0;
    uint16_t uieq_errcnt_d0[EQ_NUMB]={0xffff};
   // uint16_t uieq_errcnt_d1[EQ_NUMB]=0xffff;
   // uint16_t uieq_errcnt_d2[EQ_NUMB]=0xffff;
    vdRxRst(_lt_GlobalStruct);
    vdPiRst(_lt_GlobalStruct);
    ucread_var = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23 ) | 0x02;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23, ucread_var );
    do
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K30EQ_level[eq_stage[0]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K30EQ_level[eq_stage[1]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K30EQ_level[eq_stage[2]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xdf ); //reset error counter
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );
        delay_1ms(10);
        if(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]]	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x67 );
            //printf( "\r\nm=%x",(uint16_t)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x66 );   /*read D0 error count */

         //   uieq_errcnt_d1[eq_stage[1]]	= lt_ReadI2C_Byte( 0x69 );
          //  uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] << 8;
          //  uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] + lt_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]	= lt_ReadI2C_Byte( 0x6b );
          //  uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] << 8;
           // uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] + lt_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
      	 eq_stage[1]++;
        eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMB);
    for(i=0; i<EQ_NUMB; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
	//if(uieq_errcnt_d1[i]==0x0000)
     //   {
         //   eq_store[1][eq_count[1]]=i;
         //   eq_count[1]++;
      //  }
	// if(uieq_errcnt_d2[i]==0x0000)
       // {
          //  eq_store[2][eq_count[2]]=i;
          //  eq_count[2]++;
      //  }
    }
    //printf( "\r\nd0 eq=%x",(uint16_t)eq_count[0] );
    if(eq_count[0]==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K30EQ_level[1] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K30EQ_level[1] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K30EQ_level[1] );
    }
    else if(eq_count[0]%2==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K30EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K30EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[1] );
   // if(eq_count[1]==0)
   // {
   //     lt_WriteI2C_Byte( 0xd6, l_4K30EQ_level[1] );
    //}
   // else if(eq_count[1]%2==0)
  //  {
    //    lt_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[1][(eq_count[1]/2)]] );
  //  }
   // else
   // {
    //    lt_WriteI2C_Byte( 0xd6, l_4K30EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
   // }
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[2] );
  //  if(eq_count[2]==0)
  //  {
   //     lt_WriteI2C_Byte( 0xd7, l_4K30EQ_level[1] );
   // }
   // else if(eq_count[2]%2==0)
   // {
   //     lt_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[2][(eq_count[2]/2)]] );
   // }
   // else
  //  {
   //     lt_WriteI2C_Byte( 0xd7, l_4K30EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
   // }
    printf( "\r\n[%d] d0 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5) );
    printf( "\r\n[%d] d1 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6) );
    printf( "\r\n[%d] d2 eq=%.2x",_lt_GlobalStruct->chipNo, (uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7) );
}


void Auto_EQ_4k60(LT_GLOBAL* _lt_GlobalStruct)
{
    uint8_t i=0;
    uint8_t eq_stage[3]={0};
    uint8_t eq_count[3]={0};
    uint8_t eq_store[3][EQ_NUMC]={0};
    uint8_t ucread_var=0;
    uint16_t uieq_errcnt_d0[EQ_NUMC]={0xffff};
   // uint16_t uieq_errcnt_d1[EQ_NUMC]=0xffff;
   // uint16_t uieq_errcnt_d2[EQ_NUMC]=0xffff;
    vdRxRst(_lt_GlobalStruct);
    vdPiRst(_lt_GlobalStruct);
    ucread_var = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23 ) | 0x02;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x23, ucread_var );
    do
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K60EQ_level[eq_stage[0]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K60EQ_level[eq_stage[1]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K60EQ_level[eq_stage[2]] );
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xdf ); //reset error counter
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x03, 0xff );
        delay_1ms(10);
        if(lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x5e ) & 0x07 )
        {
            uieq_errcnt_d0[eq_stage[0]]	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x67 );
            //printf( "\r\nm=%x",(uint16_t)uieq_errcnt_d0[eq_stage[0]] );
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] << 8;
            uieq_errcnt_d0[eq_stage[0]]	= uieq_errcnt_d0[eq_stage[0]] + lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x66 );   /*read D0 error count */
		//printf( "\r\nm=%x",(uint16_t)uieq_errcnt_d0[eq_stage[0]] );
           // uieq_errcnt_d1[eq_stage[1]]	= lt_ReadI2C_Byte( 0x69 );
           // uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] << 8;
            //uieq_errcnt_d1[eq_stage[1]]	= uieq_errcnt_d1[eq_stage[1]] + lt_ReadI2C_Byte( 0x68 );   /*read D0 error count */

           // uieq_errcnt_d2[eq_stage[2]]	= lt_ReadI2C_Byte( 0x6b );
           // uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] << 8;
            //uieq_errcnt_d2[eq_stage[2]]	= uieq_errcnt_d2[eq_stage[2]] + lt_ReadI2C_Byte( 0x6a );   /*read D0 error count */
        }
        eq_stage[0]++;
        eq_stage[1]++;
       eq_stage[2]++;
    }
    while(eq_stage[0]<EQ_NUMC);
    for(i=0; i<EQ_NUMC; i++)
    {
        if(uieq_errcnt_d0[i]==0x0000)
        {
            eq_store[0][eq_count[0]]=i;
            eq_count[0]++;
        }
	//if(uieq_errcnt_d1[i]==0x0000)
        //{
           // eq_store[1][eq_count[1]]=i;
           // eq_count[1]++;
      //  }
	// if(uieq_errcnt_d2[i]==0x0000)
       // {
          //  eq_store[2][eq_count[2]]=i;
          //  eq_count[2]++;
       // }
    }
    //printf( "\r\nd0 eq=%x",(uint16_t)eq_count[0] );
    if(eq_count[0]==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K60EQ_level[2] );
		 lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K60EQ_level[2] );
		  lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K60EQ_level[2] );
    }
    else if(eq_count[0]%2==0)
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K60EQ_level[eq_store[0][(eq_count[0]/2)]] );
    }
    else
    {
        lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7, l_4K60EQ_level[eq_store[0][(eq_count[0]-1)/2]] );
    }
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[1] );
    //if(eq_count[1]==0)
    //{
    //    lt_WriteI2C_Byte( 0xd6, l_4K60EQ_level[2] );
    //}
    //else if(eq_count[1]%2==0)
    //{
     //   lt_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[1][(eq_count[1]/2)]] );
   // }
    //else
    //{
     //   lt_WriteI2C_Byte( 0xd6, l_4K60EQ_level[eq_store[1][(eq_count[1]-1)/2]] );
    //}
    
    //printf( "\r\nd1 eq=%x",(uint16_t)eq_count[2] );
   // if(eq_count[2]==0)
    //{
    //    lt_WriteI2C_Byte( 0xd7, l_4K60EQ_level[2] );
   // }
    //else if(eq_count[2]%2==0)
    //{
    //    lt_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[2][(eq_count[2]/2)]] );
    //}
    //else
   // {
   //     lt_WriteI2C_Byte( 0xd7, l_4K60EQ_level[eq_store[2][(eq_count[2]-1)/2]] );
   // }
    printf( "\r\n[%d] d0 eq=%.2x",_lt_GlobalStruct->chipNo ,(uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd5) );
    printf( "\r\n[%d] d1 eq=%.2x",_lt_GlobalStruct->chipNo ,(uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd6) );
    printf( "\r\n[%d] d2 eq=%.2x",_lt_GlobalStruct->chipNo ,(uint16_t)lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xd7) );
}


void vdRxRst( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x02, 0xfe );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x02, 0xff );
}


void vdPiRst( LT_GLOBAL* _lt_GlobalStruct )
{
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xf0 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );
}


uint32_t ulFreqIndicate1( LT_GLOBAL* _lt_GlobalStruct )
{
    uint32_t	Value		= 0;
    uint8_t	TempValue	= 0x00;
    uint8_t   read_value=0x00;
    read_value=lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x06 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x06, read_value&0xdf );
    Value		= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xf2 ) & 0x07;
    Value		= Value << 8;
    TempValue	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xf3 );
    Value		|= TempValue;
    Value		= Value << 8;
    TempValue	= 0x00;
    TempValue	= lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xf4 );
    Value		|= TempValue;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x06, read_value|0x20 );
    return(Value);
}

void vdHdmiVerStatusDet( LT_GLOBAL* _lt_GlobalStruct )
{
    uint8_t ucTMDS_Configuration;
    //delay_1ms( 300 );
    ucTMDS_Configuration = _lt_GlobalStruct->data_received[1];
    if ( (ucTMDS_Configuration & 0x02) == 0x02 ) /*M=1 HDMI2.0 */
    {
        _lt_GlobalStruct->g_bFlagHdmi20True = true;
        //printf( "\r\n[%d] HDMI2.0!", _lt_GlobalStruct->chipNo);
    }
    else
    {
        _lt_GlobalStruct->g_bFlagHdmi20True = false;
        //printf( "\r\n[%d] HDMI1.4!", _lt_GlobalStruct->chipNo);
    }
}

void vdRxTermCalibration( LT_GLOBAL* _lt_GlobalStruct )
{
    uint8_t reterm = 0;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xC9, 0x04 );       /*power on */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xC9, 0x14 );       /*rst */
    delay_1ms( 5 );
    reterm = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xf1 ) | 0x80;
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xC9, 0x1C );
    reterm = reterm + 0x30;
    printf( "\r\n[%d] Rterm =%x",_lt_GlobalStruct->chipNo,(uint16_t)reterm);
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xc7, reterm );
}

void vdTxStatusSet(LT_GLOBAL* _lt_GlobalStruct)
{
    uint32_t	freq_val;
    uint8_t	read_data;
    //uint8_t	TX_SWING;
    uint8_t	TX_TAP_SWING;
    uint8_t	TX_Enable;
    if ( _lt_GlobalStruct->g_bFlagHdmi20True )
    {
        //TX_SWING	= TX_SWING_600MHZ;
        TX_TAP_SWING	= TX_TAP_SWING_600MHZ;
        TX_Enable	= TX_PREEMPHASIS;
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe4, 0x5f );
		lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe5, 0x08 ); //bit5~bit0
    }
    else
    {
        freq_val = ulFreqIndicate(_lt_GlobalStruct);
        if ( freq_val < FREQ_DIV_200MHZ )
        {
            //TX_SWING	= TX_SWING_200MHZ;
            TX_TAP_SWING	= TX_TAP_SWING_200MHZ;
            TX_Enable	= TX_NOPREEMPHASIS;
			lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe4, 0x40 ); 
			lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe5, 0x00 ); //
        }
        else
        {
            //TX_SWING	= TX_SWING_300MHZ;
            TX_TAP_SWING	= TX_TAP_SWING_300MHZ;
            TX_Enable	= TX_NOPREEMPHASIS;
			lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe4, 0x48 ); //
			lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xe5, 0x08 ); //
        }
    }
    //lt_WriteI2C_Byte( 0xe4, 0x58 );
    //lt_WriteI2C_Byte( 0xe5, 0x20 );
    /*TX0 */
    read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa2 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa2, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa4 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa4, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa5 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa5, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = lt_ReadI2C_Byte( 0xa6 ); */
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0xa6, TX_Enable );
    /*TX1 */
#if 0
    read_data = lt_ReadI2C_Byte( 0xab );
    lt_WriteI2C_Byte( 0xab, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xad );
    lt_WriteI2C_Byte( 0xad, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xae );
    lt_WriteI2C_Byte( 0xae, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = lt_ReadI2C_Byte( 0xaf ); */
    lt_WriteI2C_Byte( 0xaf, TX_Enable );
    /*TX2 */
    read_data = lt_ReadI2C_Byte( 0xb4 );
    lt_WriteI2C_Byte( 0xb4, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xb6 );
    lt_WriteI2C_Byte( 0xb6, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xb7 );
    lt_WriteI2C_Byte( 0xb7, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = lt_ReadI2C_Byte( 0xb8 ); */
    lt_WriteI2C_Byte( 0xb8, TX_Enable );
    /*TX3 */
    read_data = lt_ReadI2C_Byte( 0xbd );
    lt_WriteI2C_Byte( 0xbd, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xbf );
    lt_WriteI2C_Byte( 0xbf, (read_data & 0xc0) | TX_TAP_SWING );
    read_data = lt_ReadI2C_Byte( 0xc0 );
    lt_WriteI2C_Byte( 0xc0, (read_data & 0xc0) | TX_TAP_SWING );
    /*read_data = lt_ReadI2C_Byte( 0xc1 ); */
    lt_WriteI2C_Byte( 0xc1, TX_Enable );
#endif
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x02, 0xe0 );//reset RX TX logic
    delay_1ms( 1 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x02, 0xff ); //reset RX TX logic
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xf0 );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0x7f );       /*close for data syc */
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0x7f );       /*close for data syc */
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0x7f );       /*close for data syc */
    delay_1ms( 10);
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0xff );
    delay_1ms( 10 );
    lt_WriteI2C_Byte(_lt_GlobalStruct->i2c_periph, 0x04, 0x7f );       /*close for data syc */
}

int8_t lt_WriteI2C_Byte(uint32_t i2c_periph, uint8_t reg, uint8_t data)
{
    uint8_t buf[2]={0};

    buf[0]=reg;
    buf[1]=data;

    return I2C_Master_BufferWrite(i2c_periph, buf, 2, LT86101UXE);
}

int8_t lt_ReadI2C_Byte(uint32_t i2c_periph, uint8_t reg)
{
    uint8_t buf=reg;
	uint8_t	result=0;

    result = I2C_Master_BufferRead(i2c_periph, &buf, 1, LT86101UXE);
	
		if(result!=0)
		{
			printf("I2c read error\r\n");
		}
    return buf;
}

int8_t lt_WriteI2C_Bytes(uint8_t reg, uint8_t *p_data, uint8_t len)
{
    return 0;
}
/*
void test_86101(void)
{
	
	printf("read reg=%X\r\n",lt_ReadI2C_Byte(0xe7));

}
*/
